var headTop = "";

var bottom = "";

	//获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/uimcardprj
    // var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1) +"/";
	var projectName = "";

var pageInit = function(){
	var createMenu = function(options){
		var mainSelect = options.main;
		var subSelect = options.sub;
		var html = "<ul class='nav nav-list'><li><a href='" + globalVariable.getValue({code:"PROJECT_NAME"}) + "pages/main.html'>";
		html += "<i class='icon-dashboard'></i><span class='menu-text'> 控制台 </span></a></li>";
		//用户管理
		// html += "<li><a href='#' class='dropdown-toggle'><i class='icon-user-md'></i><span class='menu-text'>用户管理</span>";
		// html += "<b class='arrow icon-angle-down'></b><b class='arrow icon-angle-down'></b></a>";
		// html += "<ul class='submenu'><li><a href='" + projectName + "/admin/pages/user/user_list.html'><i class='icon-double-angle-right'></i>用户列表</a></li>";
		// // html += "<li><a href='" + globalVariable.getValue({code:"PROJECT_NAME"}) +"pages/area/area_add.html'><i class='icon-double-angle-right'></i>区域添加</a></li>";
		// html += "</ul></li>";
		//成就管理
		// html += "<li><a href='#' class='dropdown-toggle'><i class='icon-search'></i><span class='menu-text'>成就管理</span>";
		// html += "<b class='arrow icon-angle-down'></b><b class='arrow icon-angle-down'></b></a>";
		// html += "<ul class='submenu'><li><a href='" + projectName + "/admin/pages/achievement/ach_list.html'><i class='icon-double-angle-right'></i>成就列表</a></li>";
		// html += "</ul></li>";
		//题库管理
		// html += "<li><a href='#' class='dropdown-toggle'><i class='icon-unlink'></i><span class='menu-text'>题库管理</span>";
		// html += "<b class='arrow icon-angle-down'></b><b class='arrow icon-angle-down'></b></a>";
		// html += "<ul class='submenu'><li><a href='" + projectName + "/admin/pages/questions/question_list.html'><i class='icon-double-angle-right'></i>题库列表</a></li>";
		// // html += "<li><a href='" + globalVariable.getValue({code:"PROJECT_NAME"}) + "pages/goods/goods_list.html'><i class='icon-double-angle-right'></i>商品列表</a></li>";
		// html += "</ul></li>";
		//留言配置管理
		// html += "<li id='content_menu'><a href='#' class='dropdown-toggle'><i class='icon-reorder'></i><span class='menu-text'>留言配置</span>";
		// html += "<b class='arrow icon-angle-down'></b><b class='arrow icon-angle-down'></b></a>";
		// html += "<ul class='submenu'><li><a href='" + projectName + "/admin/pages/msg/msg_list.html'><i class='icon-double-angle-right'></i>留言列表</a></li>";
		// html += "</ul></li>";
		//任务配置管理
		html += "<li><a href='#' class='dropdown-toggle'><i class='icon-bar-chart'></i><span class='menu-text'>用户数据管理</span>";
		html += "<b class='arrow icon-angle-down'></b><b class='arrow icon-angle-down'></b></a>";
		html += "<ul class='submenu'><li><a href='" + projectName + "/pages/record/record_user_list.html'><i class='icon-double-angle-right'></i>用户数据列表</a></li>";
		html += "</ul></li>";
		//结束
		html += "</ul>"
		$("#menu_list").append(html);
		if(!isNaN(mainSelect)){
			var $mainNode = $("#menu_list").find("li").eq(mainSelect);
			$mainNode.addClass("active open");
			if(!isNaN(subSelect)){
				$mainNode.find("ul").find("li").eq(subSelect).addClass("active open");
			}
		}
	}
	return{
		createTopBar:function(option){
			var avatarIcon = "";
			if(option.nickname===null){
				location.href = globalVariable.getValue({code:"PROJECT_NAME"}) + "pages/login.html";
			}
			if(typeof(option.avatar)=="string"){
				
				if(option.avatar == "undefined"){
					avatarIcon = "/assets/avatars/user.jpg";
				}else{
					avatarIcon = option.avatar;
				}
			}else{
				avatarIcon = option.avatar;
			}
			var topBar = "<div class='navbar-container' id='navbar-container'>";
			topBar += "<div class='navbar-header pull-left'><a href='#' class='navbar-brand'>";
			topBar += "<small><i class='icon-leaf'></i>" + globalVariable.getValue({code:"COMPANY_NAME"}) +"</small></a></div>";
			topBar += "<div class='navbar-header pull-right' role='navigation'>";
			topBar += "	<ul class='nav ace-nav'><li class='light-blue'><a data-toggle='dropdown' href='#' class='dropdown-toggle'>";
			topBar += "<img class='nav-user-photo' src='" + avatarIcon + "' alt='" + option.nickname + "Photo'/>";
			topBar += "<span class='user-info'>";
			topBar += "<small>欢迎光临, </small>Admin</span><i class='icon-caret-down'></i></a>";
			topBar += "<ul class='user-menu pull-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close'><li><a href='#'>";
			// topBar += "<i class='icon-cog'></i>设置</a></li>";
			// topBar += "<li><a href='#'><i class='icon-user'></i>个人资料</a></li>";
			topBar += "<li class='divider'></li><li><a href='#' id='logout'><i class='icon-off'></i>退出</a></li></ul></li></ul></div></div>";
			if(option.topElement){
				$("#navbar").append(topBar);
			}
			$("#logout").on("click",function(){
				// sendRequest.withToken({
				// 	url:"account/logout",
				// 	type:"POST"
				// });
				location.href = projectName + "/admin/pages/login.html";
			});
		},
		createMenuBar:function(options){
			createMenu(options);
		
		},
		getHtmlParam:function(key){
			var result=location.search.substring(1);
			var params = result.split("&");
			if(params.length>0){
				$.each(params,function(index,value){
					if(value.indexOf(key) >= 0){
						result = value.replace(key + '=','');
						return true;
					}
				})
			}
			return result;
		}
	}
}();

//发送服务端请求
var sendRequest = function(){
	//服务端请求以json方式返回
	var baseAjax = function(options){
		var tmpSend = $.ajax({
			url:options.url || "",
			type:options.method || "POST",
			async:options.async || true,
			data:options.data || {},
			//dataType:options.dataType || "json",
			beforeSend:function(XMLHttpRequest){
				if(options.beforeSendType){
					if(options.beforeSendType==="ajax"){
					//options.beforeSend();
						XMLHttpRequest.setRequestHeader("RequestType", "ajax"); 
						XMLHttpRequest.setRequestHeader( "Content-Type", "text/html;charset=UTF-8" );
					}
				}
				if(options.withToken){
					var tokenCode = sessionStorage.getItem("tokenCode");
					XMLHttpRequest.setRequestHeader("token", tokenCode);
				}
			},
			success:function(responseText,status,xhr){

				if(typeof options.callback === "function"){
					if(xhr.getResponseHeader("sessionstatus")==="timeout"){
						location.href = projectName + "pages/login.html";
					}
					if(responseText.code==="-2000"){
						console.log(responseText.code);
						location.href = projectName + "pages/login.html";
					}
					// var obj = {};
//					if(obj.code && typeof(obj.code)==="string" && obj.code.trim().length>0 && obj.code==="sessionTimeOut"){
//						location.href = projectName + "pages/login.html";
//					}
// 					if(typeof responseText == "string"){
// 						obj = jQuery.parseJSON(responseText);
// 					}else if(typeof responseText == "objcet"){
// 						obj = responseText;
// 					}
					options.callback(responseText);
				};
			},
			complete:function(){
				if(typeof options.complete ==="function"){
					options.complete();
				}
			},
			error:function(){
//				if(typeof options.errorMethod === "function"){
//					options.errorMethod();
//				}else{
//					location.href = projectName + "/error500.jsp";
//				}
			}
		});
		return tmpSend;
	};
	return{
		withToken:function(options){
			options.withToken = true;
			return baseAjax(options);
		},
		withOutToken:function(options){
			options.withToken = true;
			return baseAjax(options);
		}
	}
}();


//静态资源根据邮箱跳转
var globalVariable = (function(){
	//获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8083
    var localhostPaht = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/uimcardprj
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1) +"/";
	var hash={   
		'COMPANY_NAME':'百陌数据管理平台',
		'PROJECT_NAME':projectName
	};
	return{
		getValue:function(option){
    		for (var j in hash){   
    			option.value=hash[option.code];
    		}
    		return option.value;
		}
	}
}());

var spin = function(spinOptions){
	var opts = {
		  lines: 13, // The number of lines to draw
		  length: 6, // The length of each line
		  width: 4, // The line thickness
		  radius: 10, // The radius of the inner circle
		  corners: 1, // Corner roundness (0..1)
		  rotate: 0, // The rotation offset
		  color: '#000', // #rgb or #rrggbb
		  speed: 1.1, // Rounds per second
		  trail: 66, // Afterglow percentage
		  shadow: false, // Whether to render a shadow
		  hwaccel: false, // Whether to use hardware acceleration
		  className: 'spinner', // The CSS class to assign to the spinner
		  zIndex: 2e9, // The z-index (defaults to 2000000000)
		  top: 'auto', // Top position relative to parent in px
		  left: 'auto' // Left position relative to parent in px
	};
	return{
		create:function(opt){
			var target = document.getElementById(opt);
			var spinner = new Spinner(opts).spin(target);
		}
	}
}();